<?php
namespace Api\Controller;
use Think\Controller;
class MasterCenterController extends CommonController{

    //实例化师傅
    protected $currentMaster;
    
    function _initialize() {
        parent::_initialize();
        
        //判断token是否存在
        $token = I('post.token');

        $userMap['token'] = $token;
        $userMap['is_master'] = C("DEFAULT_YN.YES");

        $user = $this->userModel->where($userMap)->find();

        if(isNullOrEmpty($user)) {
            $this->errorReturn('ERROR_TOKEN');
        }

        $this->currentMaster = $user;
        
    }

    //拒绝订单
    public function refuseOrder() {

        $orderId = I("post.orderId");

        $order = $this->orderModel->queryOrderInfoByMasterId($orderId, $this->currentMaster["user_id"]);

        if($order["status"] != C("ORDER_STATUS.WAITING_CONFIRM")) {
            $this->errorReturn('ERROR_ORDER_NOT_FOUND');
        }

        $orderData["status"] = C("ORDER_STATUS.CONFIRM_CANCEL");

        $this->orderModel->where(array("order_id"=>$order["order_id"]))->save($orderData);

        $this->response($this->returnData, 'json');

    }

    //确认接单
    public function confirmOrder() {

        $orderId = I("post.orderId");

        $order = $this->orderModel->queryOrderInfoByMasterId($orderId, $this->currentMaster["user_id"]);

        if($order["status"] != C("ORDER_STATUS.WAITING_CONFIRM")) {
            $this->errorReturn('ERROR_ORDER_NOT_FOUND');
        }

        $orderData["status"] = C("ORDER_STATUS.WAITING_PAY");

        $this->orderModel->where(array("order_id"=>$order["order_id"]))->save($orderData);

        $this->response($this->returnData, 'json');

    }

    //获取我的订单列表
    public function myOrderList() {

        $pageNum = I('post.pageNum') ? I('post.pageNum') : C('DEFAULT_PAGE');

        $condition["status"] = I("post.status");
        $condition["master_id"] = $this->currentMaster["user_id"];

        $list = $this->orderModel->queryListByMasterAndStatus($condition, $pageNum);

        $this->returnData['data']['list'] = $list;

        $this->response($this->returnData, 'json');

    }

    //获取我的订单详情
    public function myOrderInfo() {

        $orderId = I("post.orderId");

        $order = $this->orderModel->queryOrderInfoByMasterId($orderId, $this->currentMaster["user_id"]);

        if(isNullOrEmpty($order)) {
            $this->errorReturn('ERROR_ORDER_NOT_FOUND');
        }

        $this->returnData['data']['info'] = $order;

        $this->response($this->returnData, 'json');

    }

    public function applyWithdraw() {

        $errorCode = $this->withdrawModel->applyWithdraw($this->currentMaster["user_id"]);

        $this->errorReturn($errorCode);

    }

    public function myWithdrawList() {

        $pageNum = I('post.pageNum') ? I('post.pageNum') : C('DEFAULT_PAGE');

        $list = $this->withdrawModel->queryListByMasterId($this->currentMaster["user_id"], $pageNum);

        $this->returnData['data']['list'] = $list;

        $this->response($this->returnData, 'json');

    }

    public function myWalletIndex() {

        $pageNum = I('post.pageNum') ? I('post.pageNum') : C('DEFAULT_PAGE');

        $list = $this->accountDetailModel->queryListByMasterId($this->currentMaster["user_id"], $pageNum);

        $this->returnData['data']['balance'] = $this->currentMaster["total_assets"];
        $this->returnData['data']['list'] = $list;

        $this->response($this->returnData, 'json');

    }

    public function updateMyService() {

        $regionIds = I("post.regionIds");

        $this->regionRelationshipModel->updateMasterService($regionIds, $this->currentMaster["user_id"]);

        $this->response($this->returnData, 'json');

    }

    public function updateMyServiceIndex() {

        $openList = $this->regionModel->getOpenRegionList();
        $myServiceList = $this->regionRelationshipModel->getMasterServiceList($this->currentMaster["user_id"]);

        $this->returnData['data']['open_list'] = $openList;
        $this->returnData['data']['my_list'] = $myServiceList;

        $this->response($this->returnData, 'json');

    }

    public function openMyStatus() {

        $this->currentMaster["is_busy"] = C("DEFAULT_YN.NO");

        $this->userModel->updateObj($this->currentMaster);

        $this->response($this->returnData, 'json');

    }

    public function closeMyStatus() {

        $this->currentMaster["is_busy"] = C("DEFAULT_YN.YES");

        $this->userModel->updateObj($this->currentMaster);

        $this->response($this->returnData, 'json');

    }

    public function levelDescription() {

        $list = $this->levelModel->queryForList();

        $this->returnData['data']['list'] = $list;

        $this->response($this->returnData, 'json');

    }

    public function center() {

        //用户基本信息
        $basicInfo["headimgurl"] = $this->currentMaster["headimgurl"];
        $basicInfo["nickname"] = $this->currentMaster["nickname"];
        $basicInfo["sign"] = $this->currentMaster["sign"];
        $basicInfo["order_number"] = $this->currentMaster["order_number"];
        $basicInfo["is_busy"] = $this->currentMaster["is_busy"];
        $basicInfo["total_amount"] = $this->currentMaster["total_amount"];

        $level = $this->levelModel->queryById($this->currentMaster["level_id"]);
        $basicInfo["star_number"] = $level["star_number"];

        $this->returnData['data']['basic_info'] = $basicInfo;

        //订单状态数量
        $queryMap["o.master_id"] = $this->currentMaster["user_id"];

        $queryMap["o.status"] = C("ORDER_STATUS.WAITING_CONFIRM");
        $orderInfo["waiting_confirm_number"] = $this->orderModel->getListCount($queryMap);

        $queryMap["o.status"] = C("ORDER_STATUS.WAITING_PAY");
        $orderInfo["waiting_pay_number"] = $this->orderModel->getListCount($queryMap);

        $queryMap["o.status"] = C("ORDER_STATUS.WAITING_SERVICE");
        $orderInfo["waiting_service_number"] = $this->orderModel->getListCount($queryMap);

        $queryMap["o.status"] = C("ORDER_STATUS.NORMAL_END");
        $orderInfo["end_number"] = $this->orderModel->getListCount($queryMap);

        $queryMap["o.status"] = C("ORDER_STATUS.CONFIRM_CANCEL");
        $orderInfo["cancel_number"] = $this->orderModel->getListCount($queryMap);

        $this->returnData['data']['order_info'] = $orderInfo;

        $condition["master_id"] = $this->currentMaster["user_id"];

        $condition["status"] = C("ORDER_STATUS.WAITING_CONFIRM");
        $list["waiting_confirm"] = $this->orderModel->queryListByMasterAndStatus($condition, 0);

        $condition["status"] = C("ORDER_STATUS.WAITING_PAY");
        $list["waiting_pay"] = $this->orderModel->queryListByMasterAndStatus($condition, 0);

        $condition["status"] = C("ORDER_STATUS.WAITING_SERVICE");
        $list["waiting_service"] = $this->orderModel->queryListByMasterAndStatus($condition, 0);

        $condition["status"] = C("ORDER_STATUS.NORMAL_END");
        $list["end"] = $this->orderModel->queryListByMasterAndStatus($condition, 0);

        $condition["status"] = C("ORDER_STATUS.CONFIRM_CANCEL");
        $list["cancel"] = $this->orderModel->queryListByMasterAndStatus($condition, 0);

        $this->returnData['data']['list'] = $list;

        $this->response($this->returnData, 'json');

    }

}